package cn.neusoft.service.impl;

import cn.neusoft.domain.Allocate;
import cn.neusoft.domain.Task;
import cn.neusoft.enums.*;
import cn.neusoft.exception.AppException;
import cn.neusoft.exception.AppExceptionCodeMsg;
import cn.neusoft.feignClients.OrderFeignClient;
import cn.neusoft.mapper.AllocationMapper;
import cn.neusoft.query.AllocationQuery;
import cn.neusoft.service.AllocationService;
import cn.neusoft.service.TaskService;
import cn.neusoft.util.UUIDUtils;
import cn.neusoft.vo.PageBean;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @Description:
 * @Author:邢旭升
 * @Date:2023/6/28 下午 03:00
 **/
@Service
public class AllocationServiceImpl implements AllocationService {
    @Autowired
    AllocationMapper allocationMapper;

    @Autowired
    OrderFeignClient orderFeignClient;

    @Autowired
    TaskService taskService;


    @Override
    public void addAllocate(Allocate allocate) {

        String task_id = allocate.getTask_id();
        Task task = taskService.findById(task_id);
        if (task == null){
            throw new AppException(AppExceptionCodeMsg.TASK_NOT_EXISTS);
        }

        Integer task_type = task.getTask_type();

        if(Objects.equals(task_type, TaskType.DELIVERY.getCode())){ // 正常送货

            generateNormalAllocate(task, allocate);
            allocationMapper.addAllocate(allocate);

        }else if(Objects.equals(task_type, TaskType.RETURN.getCode())){   // 退货

            generateReturnAllocate(task, allocate);
            allocationMapper.addAllocate(allocate);

        }else if(Objects.equals(task_type, TaskType.EXCHANGE.getCode())){ // 换货
            // 换货相当于正常送货+退货
            generateNormalAllocate(task, allocate);
            allocate.setRemark(AllocateType.EXCHANGE.getCode());
            allocationMapper.addAllocate(allocate);

            generateReturnAllocate(task, allocate);
            allocate.setDel_flag("1");
            allocate.setRemark(AllocateType.EXCHANGE.getCode());
            allocationMapper.addAllocate(allocate);
        }
    }

    @Override
    public void AddAllocateAnyway(Allocate allocate) {

        allocate.setAllocate_id(UUIDUtils.getUUID());
        allocate.setCreate_time(new Date());
        allocationMapper.addAllocate(allocate);
    }

    @Override
    public void deleteByAllocateId(String allocate_id) {

        allocationMapper.deleteByAllocateId(allocate_id);
    }

    @Override
    public void updateAllocate(Allocate allocate) {
        allocationMapper.updateAllocate(allocate);
    }

    @Override
    public Allocate findByAllocateId(String allocate_id) {
        return allocationMapper.findByAllocateId(allocate_id);
    }

    @Override
    @Transactional
    public PageBean<Allocate> findBypage(AllocationQuery query) {


        PageBean<Allocate> pageBean = new PageBean<>();
        PageHelper.startPage(query.getPageNow(), query.getPageSize());

        List<Allocate> allocates = allocationMapper.findByCondition(query);
        PageInfo<Allocate> pageInfo = new PageInfo<>(allocates);

        pageBean.setTotalRows(pageInfo.getTotal());
        pageBean.setTotalPages(pageInfo.getPages());
        pageBean.setRowData(pageInfo.getList());

        return pageBean;
    }

    @Override
    public PageBean<Allocate> findAllByCondition(AllocationQuery query) {
        PageBean<Allocate> pageBean = new PageBean<>();
        PageHelper.startPage(query.getPageNow(), query.getPageSize());

        List<Allocate> allocates = allocationMapper.findAllByCondition(query);
        PageInfo<Allocate> pageInfo = new PageInfo<>(allocates);

        pageBean.setTotalRows(pageInfo.getTotal());
        pageBean.setTotalPages(pageInfo.getPages());
        pageBean.setRowData(pageInfo.getList());

        return pageBean;
    }

    @Override
    @Transactional
    public PageBean<Allocate> findReturnedAllocate(AllocationQuery query){
        PageBean<Allocate> pageBean = new PageBean<>();
        PageHelper.startPage(query.getPageNow(), query.getPageSize());

        List<Allocate> allocates;
        if(Objects.equals(query.getOut_id(), Warehouse.CENTER_WAREHOUSE.getCode())){
            allocates = allocationMapper.findReturnedAllocate2(query);
        }else {
            allocates = allocationMapper.findReturnedAllocate(query);
        }


        PageInfo<Allocate> pageInfo = new PageInfo<>(allocates);

        pageBean.setTotalRows(pageInfo.getTotal());
        pageBean.setTotalPages(pageInfo.getPages());
        pageBean.setRowData(pageInfo.getList());
        return pageBean;
    }

    private void generateNormalAllocate(Task task, Allocate allocate){

        allocate.setAllocate_id(UUIDUtils.getUUID());
        allocate.setOut_id(Constants.CENTER_WAREHOUSE_ID.getCode());
        allocate.setIn_id(task.getWarehouse_id());
        allocate.setCommodity_id(task.getCommodity_id());
        allocate.setAmount(task.getAmount());
        allocate.setRemark(AllocateType.TRANSFER.getCode());
        allocate.setDel_flag(AllocateState.NOT_SHIPPED.getCode());
        allocate.setCreate_time(new Date(System.currentTimeMillis()));
        allocate.setOrder_id(task.getOrder_id());
        allocate.setTask_id(task.getTask_id());
    }

    private void generateReturnAllocate(Task task, Allocate allocate){

        allocate.setAllocate_id(UUIDUtils.getUUID());
        allocate.setOut_id(-1L);
        allocate.setIn_id(task.getWarehouse_id());
        allocate.setCommodity_id(task.getCommodity_id());
        allocate.setAmount(task.getAmount());
        allocate.setRemark(AllocateType.RETURN.getCode());
        allocate.setDel_flag("-1");
        allocate.setCreate_time(new Date(System.currentTimeMillis()));
        allocate.setOrder_id(task.getOrder_id());
        allocate.setTask_id(task.getTask_id());
    }
}
